package test04;

import java.awt.*;
import java.awt.event.*;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-14
 * Time: 10:52
 * author:GUIprreaice
 */
public class GUIprreaice {
    public static void main(String[] args) {
        new Myframe().load();
    }
}
class Myframe extends Frame {
    TextField num1;
    TextField num2;
    TextField num3;
    Button button;
    public void load() {
        //组件
        //创建3个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);

        //创建标签
        Label label = new Label("+");
        //创建按钮
        button = new Button("=");
        //为按钮设置监听
        button.addActionListener(new MyActionListener());
        button.addKeyListener(new MyKeyListener());

        //设置布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        //设置可见性
        setVisible(true);
        pack();
    }
    class MyKeyListener extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {

            //获得键盘按下的建是哪一个
            int keycode = e.getKeyCode();
            System.out.println("===");
            if( keycode == KeyEvent.VK_ENTER) {
                int a = Integer.parseInt(num1.getText());
                int b = Integer.parseInt(num2.getText());
                num3.setText("" + (a+b));
                num1.setText("");
                num2.setText("");
            }
        }
    }

    class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int a = Integer.parseInt(num1.getText());
            int b = Integer.parseInt(num2.getText());
            num3.setText("" + (a+b));
            num1.setText("");
            num2.setText("");
        }
    }
}